<?php

namespace App\Http\Controllers\Admin\Brand;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Brand;
use App\Http\Controllers\Common\CommonApi;

//mais
class brandController extends Controller
{
    public function list()
    {
    	//取数据,分页类
    	$list = Brand::select('id', 'bname', 'logo_path', 'descr')->where('state', 1)->Paginate(10);

    	//返回视图和数据和分页
    	return view('Admin/Brand/product-brand', ['list'=>$list]);
    }

    public function del(Request $request)
    {

    	$res = Brand::find($request->id);
        $res->state = 0;
        $sth = $res->save();

        if ($sth) {
            return 1;
        }else{
            return 2;
        }
    }

    public function add(Request $request)
    {
        $qiniuyun = new CommonApi();

        $res = $qiniuyun->uploadToQiNiu($request);

        if ($res['msg'] !== '上传成功') {
                return $res['msg'];
        }

        //将七牛云返回的路径拼接一下
        
        $path = 'http://owv3oi69q.bkt.clouddn.com/'.$res['data']['key'];

        $brandmod = new brand;

        $brandmod->bname = $request->bname;
        $brandmod->logo_path = $path;
        $brandmod->descr = $request->descr;
        $brandmod->state = 1;

        $oks = $brandmod->save();

        if ($oks) {
            return redirect()->action('Admin\Brand\brandController@list');
        }else{
            return '大哥你填写有错误呀';
        }
        
    }

    public function edit(Request $request)
    {
        $list = Brand::find($request->id);

        return view('Admin/Brand/product-brand-edit', ['list'=>$list]);
    }

    public function doedit(Request $request)
    {

        $qiniuyun = new CommonApi();

        $res = $qiniuyun->uploadToQiNiu($request);

        if ($res['msg'] !== '上传成功') {
                return $res['msg'];
        }

        //将七牛云返回的路径拼接一下
        
        $path = 'http://owv3oi69q.bkt.clouddn.com/'.$res['data']['key'];

        //修改品牌
        $brandmod = Brand::find($request->id);

        $brandmod->bname = $request->bname;
        $brandmod->logo_path = $path;
        $brandmod->descr = $request->descr;

        $res = $brandmod->save();

        if ($res) {
            return 1;
        }else{
            return 2;
        }
    }
}
